<?php

namespace app\admin\controller\stat;

use app\admin\controller\Controller;
use think\facade\Db;

class Mobile extends Controller
{
    /**
     * 列表.
     */
    public function successRate()
    {
        // 是否刷新最新数据
        $refresh = input('post.refresh', 0);
        $searchSupplierId = input('post.supplier_id');

        $cacheKey = 'admin.stat.mobile.successRate';
        $cacheVal = cache($cacheKey);
        if (!$refresh && $cacheVal) {
            if ($searchSupplierId) {
                foreach ($cacheVal['data'] as $k => $v) {
                    if ($v['supplier_id'] != $searchSupplierId) {
                        unset($cacheVal['data'][$k]);
                    }
                }
                $cacheVal['data'] = array_merge($cacheVal['data']);
            }
            if (!empty($cacheVal['data'])) {
                return success('操作成功', $cacheVal);
            }
        }


        $supplierList = Db::name('mobile_supplier')
            ->where('is_deleted', 0)
            ->column('name, yidong_fast, liantong_fast, dianxin_fast', 'id');
        if ($searchSupplierId) {
            $supplierIds = [$searchSupplierId];
        } else {
            $supplierIds = array_keys($supplierList);
        }
        foreach ($supplierList as $k => $v) {
            $supplierList[$k]['yidong_fast'] = json_decode($v['yidong_fast']);
            $supplierList[$k]['liantong_fast'] = json_decode($v['liantong_fast']);
            $supplierList[$k]['dianxin_fast'] = json_decode($v['dianxin_fast']);
        }
        $ispTypeList = [
            1 => [
                'name' => '移动',
                'key' => 'yidong_fast',
            ],
            2 => [
                'name' => '联通',
                'key' => 'liantong_fast',
            ],
            3 => [
                'name' => '电信',
                'key' => 'dianxin_fast',
            ],
        ];
        $amountList = [30, 50, 100, 200];
        $tmp = [];
        foreach ($supplierIds as $supplierId) {
            foreach ($ispTypeList as $ispType => $ispName) {
                foreach ($amountList as $amount) {
                    $tmp[$supplierId][$ispType][$amount]['success'] = 0;
                    $tmp[$supplierId][$ispType][$amount]['total'] = 0;
                }
            }
        }

        $list = Db::name('mobile_order_stats')
            ->field('id, isp_type, amount, status, supplier_id, agent_id')
            ->where('created_at', '>=', date('Y-m-d H:i:s', time() - 30 * 60)) // 30分钟成功率
            ->whereIn('supplier_id', $supplierIds)
            ->cursor();
        foreach ($list as $v) {
            if ($v['isp_type'] == -1) {
                continue;
            }
            $tmp[$v['supplier_id']][$v['isp_type']][$v['amount']]['total'] += 1;
            if ($v['status'] == 1) {
                $tmp[$v['supplier_id']][$v['isp_type']][$v['amount']]['success'] += 1;
            }
        }

        $data = [];
        $id = 1;
        foreach ($tmp as $supplierId => $v1) {
            foreach ($v1 as $ispType => $v2) {
                $t = [
                    'id' => $id++,
                    'supplier_id' => $supplierId,
                    'supplier_name' => $supplierList[$supplierId]['name'],
                    'isp_name' => $ispTypeList[$ispType]['name'],
                ];
                $key = $ispTypeList[$ispType]['key'];
                foreach ($v2 as $amount => $v3) {
                    if (!in_array($amount, $supplierList[$supplierId][$key])) {
                        $rate = '维护';
                    } else if ($v3['total']) {
                        $rate = number_format($v3['success'] / $v3['total'] * 100, 2) . '%';
                    } else {
                        $rate = '--';
                    }
                    $t['amount_' . $amount] = $rate;
                }
                $data[] = $t;
            }
        }
        unset($supplierIds);
        unset($tmp);

        $cacheVal = [
            'data' => $data,
            'stat_time' => date('Y-m-d H:i:s'),
            'supplier' => array_column($supplierList, 'name', 'id'),
        ];
        cache($cacheKey, $cacheVal, 300); // 缓存 5 分钟

        return success('操作成功', $cacheVal);
    }
}
